package pri.zxx.webdemo.quartzdemo.demo1;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

/**
 * @author zxx
 * @desc
 * @createTime 2019-12-27-下午 3:54
 */
@RestController
@Tag(name =  "LEARN_DEMO-quartz 定时任务测试")
public class QuartzTestDemo {


    @GetMapping(value = "/quartz")
    public void quartzNew() throws SchedulerException, InterruptedException, ParseException {
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = stdSchedulerFactory.getScheduler();

        JobDetail build = JobBuilder.newJob(JobFactoryImp.class)
                .storeDurably()
                .withIdentity("hello", "say")
                .usingJobData("url", "https://www.baidu.com")
                .build();
        JobDetail build2 = JobBuilder.newJob(JobFactoryImp.class)
                .storeDurably()
                .usingJobData("url", "https://blog.csdn.net/xlgdst/article/details/79104273")
                .withIdentity("hello2", "say")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger().startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2)
                        .repeatForever())
                .build();
        Trigger trigger2 = TriggerBuilder.newTrigger().startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())
                .build();
        //        CronTrigger cronTrigger = TriggerBuilder.newTrigger().startNow().withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("")))
        //                .forJob(build).build();
        //
        //        scheduler.scheduleJob(cronTrigger);
        scheduler.scheduleJob(build, trigger);
        scheduler.scheduleJob(build2, trigger2);
        scheduler.start();
        TimeUnit.SECONDS.sleep(10);

        scheduler.pauseJob(build2.getKey());
        scheduler.unscheduleJob(trigger2.getKey());
        scheduler.deleteJob(build2.getKey());
//        scheduler.shutdown();
    }
}
